package com.sl.au.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.EXDEcustomerViewTotal;
import com.sl.au.entity.Option;
import com.sl.au.entity.User;
import com.sl.au.repository.ExdeCustomerViewTotalRepository;
import com.sl.au.service.ExdeCustomerViewTotalService;
import com.sl.au.service.QueryService;

@Service("ExdeCustomerViewTotalService")
@Transactional
public class ExdeCustomerViewTotalServiceImpl implements ExdeCustomerViewTotalService{
	
	@Autowired
	ExdeCustomerViewTotalRepository exdeCustomerViewTotalRepository;
	
	@Autowired
	QueryService queryService;
	
	@Override
	public ExdeCustomerViewTotalRepository exdeCustomerViewTotalRepository() {
		return this.exdeCustomerViewTotalRepository;
	}
	@Override
	public List<EXDEcustomerViewTotal> getUser(int start, int size,String deptkey,String businkey,String xinyongkey,String zicankey) {
		Page<EXDEcustomerViewTotal> pages = null;
		Pageable pageable = new PageRequest(start / size, size, Sort.Direction.DESC, "createTime");
		if(xinyongkey != null && xinyongkey != "" || deptkey != null && deptkey != "" || zicankey != null && zicankey != "" || businkey != null && businkey != ""){
			pages = exdeCustomerViewTotalRepository.getUserList(deptkey,businkey,xinyongkey,zicankey,pageable);
		}else{
			pages = exdeCustomerViewTotalRepository.getUserList(pageable);
		}
		return pages.getContent();
	}

	@Override
	public long getAllUserCount(String deptkey,String businkey,String xinyongkey,String zicankey) {
		if(xinyongkey != null && xinyongkey != "" || deptkey != null && deptkey != "" || zicankey != null && zicankey != "" || businkey != null && businkey != ""){
			return exdeCustomerViewTotalRepository.getUserCount(deptkey,businkey,xinyongkey,zicankey);
		}else{
			return exdeCustomerViewTotalRepository.getUserCount();
		}
	}
	@Override
	public List<Option> getSaleArea() {
		// TODO Auto-generated method stub
		return this.exdeCustomerViewTotalRepository.getSaleArea();
	}
	
	
	@Override
	public List<EXDEcustomerViewTotal> getcustomerInfos(int start, int length, Map<String, Object> condition, User user,
			String orderName, String dir) {
		@SuppressWarnings("unchecked")
		HashMap<String, Object>numMap=(HashMap<String, Object>)condition.get("numMap");
		Map<String,Object>namedParamsMap=new HashMap<String,Object>();
		StringBuilder hql=queryService.buildCombinedQueryHql("EXDEcustomerViewTotal",null, condition, namedParamsMap,null, user);
		for(String key:numMap.keySet()){
			Object value=numMap.get(key);
			if(key=="creditLevel"){
				String[] a = value.toString().split(",");
				hql.append(" and ( 1<>1 ");
				for(int i=0;i<a.length;i++){
					if(a[i].equals("30分以下")) hql.append(" or (e.creditLevel <= 30  )");
					if(a[i].equals("31~60分")) hql.append(" or (e.creditLevel > 30 and e.creditLevel<=60 )");
					if(a[i].equals("61~90分")) hql.append(" or (e.creditLevel > 60 and e.creditLevel<=90 )");
					if(a[i].equals("91~120分")) hql.append(" or (e.creditLevel > 90 and e.creditLevel<=120 )");
					if(a[i].equals("120分以上")) hql.append(" or (e.creditLevel > 120  )");
				}
				hql.append(") "); 
			}else if(key=="asset"){
				String[] a = value.toString().split(",");
				hql.append(" and ( 1<>1 ");
				for(int i=0;i<a.length;i++){
					if(a[i].equals("'国有'")) hql.append(" or (e.asset = '国有')");
					if(a[i].equals("'外资'")) hql.append(" or (e.asset = '外资' )");
					if(a[i].equals("'股份'")) hql.append(" or (e.asset = '股份' )");
					if(a[i].equals("'民营'")) hql.append(" or (e.asset = '民营' )");
				}
				hql.append(") ");
			}/*else if(key == "business"){
				if(value.toString().equals("1")){
					hql.append("and (e."+key+" in (''))");
				}else{
					hql.append("and (e."+key+" in ("+value.toString()+"))");
				}
			}*/
			else if(key == "customerName"){
				hql.append("and (e."+key+" like '%"+value.toString()+"%')");
			}
			else{
				if (value instanceof String){
					 if(StringUtils.hasText((String)value)){
						 System.out.println(key+" + "+value.toString());
						 hql.append("and (e."+key+" in ("+value.toString()+"))");
					 }
				}
				
				
			}
		}
		String dataFilter=condition.get("dataFilter").toString();
		if(StringUtils.hasText(dataFilter)){
			//this.contractDataFilter(hql, dataFilter, user);
		}else{
			//return null;
		}
		hql.append(" order by " + orderName + " " + dir);
		System.out.println(hql.toString());
		Query query=queryService.createQueryHql(hql.toString());
		query.setFirstResult(start).setMaxResults(length);
		return queryService.executeHql(query, namedParamsMap);
	}

}
